//
// Created by weslywang on 2020/6/25.
//

#include "../common/common.h"

void sumNumbers(TreeNode *root, long &pre, int &total) {
    if (root == NULL) {
        return;
    }
    if (root->left == NULL && root->right == NULL) {
        total += pre + root->val;
    }
    long val = (pre + root->val) * 10;
    if (root->left != NULL) {
        sumNumbers(root->left, val, total);
    }
    if (root->right != NULL) {
        sumNumbers(root->right, val, total);
    }
}

int sumNumbers(TreeNode *root) {
    int result = 0;
    long pre = 0;
    sumNumbers(root, pre, result);
    return result;
}

int main() {
    Common common;
    int a[] = {1, 2, 3, 4, 5};
    vector<vector<int>> numList = common.loadData();
    for (int i = 0; i < numList.size(); i++) {
        TreeNode *root = common.makeTree(numList[i]);
//        common.printTree(root);
        cout << sumNumbers(root) << endl;
        cout << "**************" << endl;
    }
    return 0;
}